﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Web.AppCode.Sys_Config.IntiCity("Aomen");
           
            if (!IsPostBack)
            {
                Bind();
                
            }
        }
        
        public void Bind()
        {
            
            DataSet ds = (new Controller.CorpInfoShow()).GetIndexMore(Web.AppCode.Sys_Config.GetCurrentCityCode());

            DataView dvHot = ds.Tables[0].DefaultView;
            dvHot.Sort = " Hits desc";
            dvHot = GetTopDataViewRows(dvHot,3, 16);
            rptHotNoneImage.DataSource = dvHot;
            rptHotNoneImage.DataBind();

            DataView dvNew = ds.Tables[0].DefaultView;
            dvNew.Sort = " Createon desc";
            dvNew = GetTopDataViewRows(dvNew,3,16);
            rptNewNoneImage.DataSource = dvNew;
            rptNewNoneImage.DataBind();

           //餐饮美食
            DataView dvType1 = ds.Tables[0].DefaultView;
            dvType1.RowFilter = " TypeID = 1 ";
            dvType1.Sort = " Hits desc";
            dvType1 = GetTopDataViewRows(dvType1, 10);
            rptType1Hot10.DataSource = dvType1;
            rptType1Hot10.DataBind();

            //夜场 酒吧
            DataView dvType2 = ds.Tables[0].DefaultView;
            dvType2.RowFilter = " TypeID = 2 ";
            dvType2.Sort = " Hits desc";
            dvType2 = GetTopDataViewRows(dvType2, 10);
            rptType2Hot10.DataSource = dvType2;
            rptType2Hot10.DataBind();

           //娱乐休闲
            DataView dvType3 = ds.Tables[0].DefaultView;
            dvType3.RowFilter = " TypeID = 3 ";
            dvType3.Sort = " Hits desc";
            dvType3 = GetTopDataViewRows(dvType3, 10);
            rptType3Hot10.DataSource = dvType3;
            rptType3Hot10.DataBind();

            //资讯前线
            DataSet infoDs = (new Controller.InformationShow()).GetIndexInfo();
            rptNew10Info.DataSource = infoDs;
            rptNew10Info.DataBind();

            //所有的城市
            DataSet corpAllCityDS = (new Controller.CorpInfoShow()).GetIndexDiffType();
             DataView dvAllCityTop3= corpAllCityDS.Tables[0].DefaultView;
             rptAllAreaTop3.DataSource = GetTopDataViewRows(dvAllCityTop3,3);
             rptAllAreaTop3.DataBind();

             DataView dvAllCityTop3more= corpAllCityDS.Tables[0].DefaultView;
             rptAllAreaTop3More.DataSource = GetTopDataViewRows(dvAllCityTop3more, 3, 9);
             rptAllAreaTop3More.DataBind();


             DataSet couponds = (new Controller.CouponShow()).GetIndexTop6(Web.AppCode.Sys_Config.GetCurrentCityCode());
             rptCouponList.DataSource = couponds;
             rptCouponList.DataBind();
        }

       private  DataView GetTopDataViewRows(DataView dv, int n)
       {
          DataTable dt = dv.Table.Clone();
  
          for (int i = 0; i <=n - 1; i++)
          {
              if (i >= dv.Count)
              {
                  break;
              }
             dt.ImportRow(dv[i].Row);
         }
           
            return new DataView(dt, dv.RowFilter, dv.Sort, dv.RowStateFilter);
       }
       private DataView GetTopDataViewRows(DataView dv, int rows, int n)
       {
           DataTable dt = dv.Table.Clone();
           for (int i = 0; i < n - 1; i++)
           {
               if (i >= dv.Count)
               {
                   break;
               }
               if (dv.Table.Rows.Count <=(i + rows))
               {
                   dt.ImportRow(dv[i ].Row);
               }
               else
               {
                   dt.ImportRow(dv[i + rows].Row);
               }
               
           }
           return new DataView(dt, dv.RowFilter, dv.Sort, dv.RowStateFilter);
       }
    }

 
}
